package com.example.controller;

import com.example.pojo.*;
import com.example.service.CompetitionService;
import com.example.service.UserService;
import com.example.utils.ThreadLocalUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/competition")
public class CompetitionController {
    @Autowired
    private CompetitionService competitionService;
    @Autowired
    private UserService userService;
    //管理员设置比赛的种类（管理员权限）
    @RequestMapping("/addCategory")
    public Result addCategory(@RequestBody CompetitionCategory competitionCategory){
        Claims claims = ThreadLocalUtil.get();
        String username = (String) claims.get("username");
        User user = userService.findByUsername(username);
        if (user.getRole() != 3){
            return Result.error("需要管理员权限！");
        }
        competitionService.addCategory(competitionCategory);
        return Result.success();
    }
    //（组织/机构）发布征文比赛

    //查询比赛有哪些分类
    @RequestMapping("/listCategory")
    public Result listCategory(){
        List<CompetitionCategory> competitionCategoryList = competitionService.listCategory();
        return Result.success(competitionCategoryList);
    }
    //删除比赛分类
    @RequestMapping("/deleteCategory")
    public Result deleteCategory(Integer id){
        Claims claims = ThreadLocalUtil.get();
        String username = (String) claims.get("username");
        User user = userService.findByUsername(username);
        if (user.getRole() != 3){
            return Result.error("需要管理员权限！");
        }
        competitionService.deleteCategory(id);
        return Result.success();
    }
    //修改比赛分类
    @RequestMapping("/updateCategory")
    public Result updateCategory(@RequestBody CompetitionCategory competitionCategory){
        Claims claims = ThreadLocalUtil.get();
        String username = (String) claims.get("username");
        User user = userService.findByUsername(username);
        if (user.getRole() != 3){
            return Result.error("需要管理员权限！");
        }
        competitionService.updateCategory(competitionCategory);
        return Result.success();
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Competition competition) {
        Claims claims = ThreadLocalUtil.get();
        String username = (String) claims.get("username");
        User user = userService.findByUsername(username);
        if (user.getRole() != 2){
            return Result.error("需要你是组织/机构才可发起比赛！");
        }
        competition.setOrganizerId(user.getId());
        competitionService.add(competition);
        return Result.success();
    }
    //查询比赛
    @RequestMapping("/list")
    public Result list() {
        return Result.success(competitionService.list());
    }
    //更改比赛信息
    @RequestMapping("/update")
    public Result update(@RequestBody Competition competition) {
        Claims claims = ThreadLocalUtil.get();
        String username = (String) claims.get("username");
        User user = userService.findByUsername(username);
        if (user.getRole() != 2){
            return Result.error("需要你是组织/机构才可更改比赛！");
        }
        //发布者才能修改比赛信息
        Competition competition1 = competitionService.findByUserId(user.getId(),competition.getCompetitionId());
        if (competition1 == null){
            return Result.error("你没有权限修改该比赛信息！");
            }
        competitionService.update(competition);
        return Result.success();
    }
    //查看比赛详情
    @RequestMapping("/detail")
    public Result detail(Integer id) {
        Competition competition =  competitionService.detail(id);
        return Result.success(competition);
    }
    //删除比赛
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        Claims claims = ThreadLocalUtil.get();
        String username = (String) claims.get("username");
        User user = userService.findByUsername(username);
        if (user.getRole() != 2){
            return Result.error("需要你是组织/机构才可删除比赛！");
        }
        competitionService.delete(id);
        return Result.success();
    }
    //用户报名参与比赛
    @RequestMapping("/join")
    public Result join(Integer id) {
        Claims claims = ThreadLocalUtil.get();
        String username = (String) claims.get("username");
        User user = userService.findByUsername(username);
        if (user.getRole() != 1){
            return Result.error("需要你是普通用户才可参与比赛！");
        }
        competitionService.join(id,user.getId());
        return Result.success("参赛成功");
    }
    //用户取消报名比赛
    @RequestMapping("/cancel")
    public Result cancel(Integer id) {
        Claims claims = ThreadLocalUtil.get();
        String username = (String) claims.get("username");
        User user = userService.findByUsername(username);
        if (user.getRole() != 1){
            return Result.error("需要你是普通用户才可取消比赛！");
        }
        competitionService.cancel(id,user.getId());
        return Result.success("取消成功");
    }
    //用户查看已报名的比赛
    @RequestMapping("/joined")
    public Result joined() {
        Claims claims = ThreadLocalUtil.get();
        String username = (String) claims.get("username");
        User user = userService.findByUsername(username);
        if (user.getRole() != 1){
            return Result.error("需要你是普通用户才可查看比赛！");
        }
        List<Competition> competitionList = competitionService.joined(user.getId());
        return Result.success(competitionList);
    }

    //用户提交作品
    @RequestMapping("/submit")
    public Result submit(Integer competitionId, Integer articleId) {
        Claims claims = ThreadLocalUtil.get();
        String username = (String) claims.get("username");
        User user = userService.findByUsername(username);
        competitionService.submit(competitionId,articleId,user.getId());
        return Result.success();
    }

    //评委对作品评分
    @RequestMapping("/score")
    public Result score(Long submissionId, Integer score) {
        Claims claims = ThreadLocalUtil.get();
        String username = (String) claims.get("username");
        User user = userService.findByUsername(username);
        if (user.getRole() != 2){
            return Result.error("需要你是评委才可评分！");
        }
        Integer judgeId = user.getId();
        Competition competition = competitionService.findBySubmissionId(submissionId);
        if (competition == null) return Result.error("该比赛不存在！");
        //发布者才能修改比赛信息
        Competition competition1 = competitionService.findByUserId(user.getId(),competition.getCompetitionId());
        if (competition1 == null){
            return Result.error("组办方才能给比赛选手打分！");
        }
        //score只能是1~100
        if (score < 1 || score > 100) return Result.error("评分只能是1~100");
        competitionService.score(submissionId,score);
        return Result.success();
    }





}
